import { getPage } from '../../utils/config';
import api from '../../utils/api';
import { http  } from '../../utils/utils';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        getPage: getPage,
        loading: 0,
        countDown: 9,
        activeTabs: 0,
        activeId: 0,
        scrollLeft: 0,
        pageSize: 20,
        loadmore: false,
    },

    /**
     * 时间点击
     */
    dateTabsTap(e) {
        let { activeTabs, activeId, productsInfo, countDown, scrollLeft, loading } = this.data;
        let itemWidth = 0;
        const { dataset, offsetLeft } = e.currentTarget || e.target;
        
        if (dataset.index == activeTabs) return false;
        
        activeTabs = dataset.index;
        activeId = dataset.id;
        countDown = 9;

        const query = wx.createSelectorQuery().selectAll('.date-item').boundingClientRect().exec((res) => {
            res[0].forEach((item, index) => {
                if (index == dataset.index){
                    itemWidth = item.width;
                }
            });

            scrollLeft = (offsetLeft - (this.systemInfo.windowWidth) / 2) + (itemWidth / 2);

            this.setData({ scrollLeft, activeTabs, activeId, countDown });

            //获取当前是否加载过
            if (!productsInfo[activeTabs].load){
                this.setData({ loading: 0 })

                this.getProductsInfo((data) => {
                    const { activeTabs, productsInfo } = this.data;

                    //必抢
                    productsInfo[activeTabs].first.push(data[0]);
                    //商品列表
                    productsInfo[activeTabs].list = data.slice(1);

                    //请求实时热抢
                    this.getHotProductsInfo(() => {
                        //最后完成请求
                        this.setData({ productsInfo, loading: 1 });
                    });
                }, false);
            }
        });
    },

    /**
     * 获取产品
     */
    getProductsInfo (cb, a = true){
        const { activeId, activeTabs, pageSize, productsInfo } = this.data;

        http.get(api.default, {
            data: {
                action: 'getmsproducts',
                cid: 0,
                activityId: activeId,
                pagesize: pageSize,
                pageindex: productsInfo[activeTabs].page,
            }
        }, a).then((res) => {
            const { data } = res.data;
            //已加载过
            if(!productsInfo[activeTabs].load){
                productsInfo[activeTabs].load = true;
            }
            //没有下一页
            if(data.length < pageSize){
                productsInfo[activeTabs].loaddone = true;
                productsInfo[activeTabs].nextPage = false;
            }
            this.setData({ productsInfo });

            cb && cb(data);
        });
    },

    /**
     * 获取实时热抢
     */
    getHotProductsInfo(cb) {
        let { activeId, prid, countDown, hotsProducts } = this.data;

        http.get(api.default, {
            data: {
                action: 'GetMSRecommendProducts',
                prid: prid,
                activityId: activeId,
            }
        }, false).then((res) => {
            let { data } = res.data;
            let result = [];
            let size = 4;
            let current = 0;

            for (let i = 0; i < Math.ceil(data.length / size); i++){
                let start = i * size;
                let end = start + size;
                result.push(data.slice(start, end));
            }

            //实时热卖产品默认组
            hotsProducts = result[current];

            //倒计时是否存在
            if(this.timer || this.timer !== undefined){
                clearInterval(this.timer);
            }

            //倒计时
            this.timer = setInterval(() => {
                
                countDown--;
                if(countDown < 0){
                    current++;
                    if (current > result.length - 1){
                        current = 0;
                    }
                    hotsProducts = result[current];
                    countDown = 9;
                    this.setData({ hotsProducts });
                }

                this.setData({ countDown });
            }, 1000);
            
            this.setData({ hotsProducts });

            cb && cb(result);
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        /** 
         * 判断是否为空
         */
        if(JSON.stringify(options) !== '{}'){
            this.setData({
                prid: options.prid
            })
        }
        
        /**
         * 获取设备信息
         */
        this.systemInfo = wx.getSystemInfoSync();

        /**
         * 获取限时活动时间
         */
        http.get(api.default, {
            data: {
                action: 'getmslist',
            }
        }, false).then((res) => {
            const { data } = res.data;
            let { activeId } = this.data;
            //产品集合
            const products = [];

            //根据时间处理
            data.forEach((item, index) => {
                //产品集合处理
                let arr = {
                    current: false,
                    id: item.model.ID,
                    page: 1,
                    list: [],
                    first: [],
                    nextPage: !false,
                    load: false,
                    loaddone: false,
                }
                //取出当前进行中
                if ((item.begintime - item.datenow) < (60 * 60 * 24)){
                    arr.current = true;
                    item.current = true;
                    activeId = item.model.ID
                }else{
                    item.current = false;
                }
                products.push(arr);
            });

            this.setData({ activeId, productsInfo: products, msList: data });

            //获取当前活动商品
            this.getProductsInfo((data) => {
                const { activeTabs, productsInfo } = this.data;

                //必抢
                productsInfo[activeTabs].first.push(data[0]);
                //商品列表
                productsInfo[activeTabs].list = data.slice(1);

                //请求实时热抢
                this.getHotProductsInfo((data) => {
                    //最后完成请求
                    this.setData({ productsInfo, loading: 1 });
                });
            }, false);
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        let { activeTabs, activeId, productsInfo, loadmore } = this.data;

        if (productsInfo[activeTabs].nextPage && !loadmore){
            productsInfo[activeTabs].page++;
            loadmore = true;
            
            this.setData({ productsInfo, loadmore });

            this.getProductsInfo((data) => {
                productsInfo[activeTabs].list = productsInfo[activeTabs].list.concat(data);
                loadmore = false;

                this.setData({ productsInfo, loadmore })
            }, false);
        }
    }
})